﻿// --- Copyright (c) 2006-2008 Stefan Kyntchev ---
// This software is written and copyrighted by Stefan Kyntchev 
// and BeyondPod Team members. All rights are reserved.
// Author contact: support@beyondpod.mobi
// ------------------------------------------------------------------------
// This file is part of BeyondPod RSS Feed Reader and Podcast manager
// (www.codeplex.com/BeyondPod) 
// BeyondPod is free software: you can redistribute it and/or modify 
// it under the terms of the GNU General Public License as published by 
// the Free Software Foundation, either version 3 of the License, or 
// (at your option) any later version. 
//  
// BeyondPod is distributed in the hope that it will be useful, 
// but WITHOUT ANY WARRANTY; without even the implied warranty of 
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
// GNU General Public License for more details. 
//  
// You should have received a copy of the GNU General Public License
// along with BeyondPod. If not, see <http://www.gnu.org/licenses/>
// ------------------------------------------------------------------------
// COPYRIGHT NOTICE: This copyright notice may NOT be removed, obscured or modified 
// without written consent from the author.
// --- End copyright notice --- 


using System;
using System.Drawing;
using System.Runtime.InteropServices;
using svs.Mobile.BeyondMedia.UiCore.GDI;

namespace svs.Mobile.BeyondMedia.UiCore.KineticControls
{
	public class ImageData : IDisposable
		{
			// Pointer to an external unmanaged resource.
			private bool disposed;
			private IntPtr _imageHandle = IntPtr.Zero;
			private readonly Size _ImageSize;
			private Color _TransparentColor = Color.Empty;

			public IntPtr ImageHandle
			{
				get { return _imageHandle; }
			}

			public Size ImageSize
			{
				get { return _ImageSize; }
			}

			public Color TransparentColor
			{
				get { return _TransparentColor; }
				set { _TransparentColor = value; }
			}

			// Track whether Dispose has been called.

			// The class constructor.
			public ImageData(string imagePath)
			{
				if(string.IsNullOrEmpty(imagePath))
					return;

				_imageHandle = Win32.SHLoadImageFile(imagePath);
				if (_imageHandle != IntPtr.Zero)
				{
					var bm = new Win32.BITMAP();
					Win32.GetObject(_imageHandle, Marshal.SizeOf(bm), ref bm);
					_ImageSize.Height = bm.bmHeight;
					_ImageSize.Width = bm.bmWidth;
					ViewportSize = ImageSize;
				}
			}

			public ImageData(string ImagePath, Color transparentColor) : this(ImagePath)
			{
				_TransparentColor = transparentColor;
			}


			public Point ImageOffset { get; set; }

			public Size ViewportSize { get; set; }

			// Implement IDisposable.
			// Do not make this method virtual.
			// A derived class should not be able to override this method.

			#region IDisposable Members

			public void Dispose()
			{
				Dispose(true);
				// This object will be cleaned up by the Dispose method.
				// Therefore, you should call GC.SupressFinalize to
				// take this object off the finalization queue 
				// and prevent finalization code for this object
				// from executing a second time.
				GC.SuppressFinalize(this);
			}

			#endregion

			// Dispose(bool disposing) executes in two distinct scenarios.
			// If disposing equals true, the method has been called directly
			// or indirectly by a user's code. Managed and unmanaged resources
			// can be disposed.
			// If disposing equals false, the method has been called by the 
			// runtime from inside the finalizer and you should not reference 
			// other objects. Only unmanaged resources can be disposed.
			private void Dispose(bool disposing)
			{
				// Check to see if Dispose has already been called.
				if (!disposed)
				{
					// Call the appropriate methods to clean up 
					// unmanaged resources here.
					// If disposing is false, 
					// only the following code is executed.
					Win32.DeleteObject(_imageHandle);
					_imageHandle = IntPtr.Zero;
				}
				disposed = true;
			}

			// Use C# destructor syntax for finalization code.
			// This destructor will run only if the Dispose method 
			// does not get called.
			// It gives your base class the opportunity to finalize.
			// Do not provide destructors in types derived from this class.
			~ImageData()
			{
				// Do not re-create Dispose clean-up code here.
				// Calling Dispose(false) is optimal in terms of
				// readability and maintainability.
				Dispose(false);
			}
		}
}
